part of base.widgets;

class LinearTextProgress extends StatefulWidget {
  final BoxDecoration? valueDecoration;
  final BoxDecoration? decoration;
  final double? height;
  final Color? backgroundColor;
  final Color? valueColor;
  final Color? valueTextColor;
  final Color? labelTextColor;
  final double? value;
  final String? valueAlias;
  final String? text;
  final int maxValue;

  ///目前两种样式
  final int type;

  const LinearTextProgress(
      {Key? key,
      this.value,
      this.backgroundColor,
      this.valueColor,
      this.text,
      this.height,
      this.decoration,
      this.valueDecoration,
      this.type = 0,
      this.valueTextColor,
      this.labelTextColor,
      this.maxValue = 100,
      this.valueAlias})
      : super(key: key);

  State<StatefulWidget> createState() {
    return LinearTextProgressState();
  }
}

class LinearTextProgressState extends State<LinearTextProgress> {
  Color get labelTextColor => widget.labelTextColor ?? Color(0xff999999);

  Color get valueTextColor => widget.valueTextColor ?? Color(0xff999999);

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
      // print("maxWidth=== ${constraints.maxWidth}");
      var progressValue = (widget.value ?? 0) * constraints.maxWidth;
      // print("calcTextSize=== $progressValue");

      int value = ((widget.value ?? 0) * 100).toInt();
      var valueFontSize = (widget.height ?? 10) - 2;
      if (widget.type == 1) {
        print(progressValue);
        // print("progressValue=== $progressValue");
        var text = calcTextSize("$value", fontSize: valueFontSize);
        // print("progressValue=== $text");

        progressValue -= text;
        // print("calcTextSize=== $text");
        print("calcTextSize=== $progressValue");
      }
      return Stack(
        alignment: Alignment.center,
        children: <Widget>[
          SizedBox(
              height: widget.height,
              child: _getWidgetByType(widget.type, valueFontSize, value,
                  progressValue, constraints)),
          _leftText()
        ],
      );
    });
  }

  _getWidgetByType(int type, double valueFontSize, int value,
      double progressValue, BoxConstraints constraints) {
    if (type == 4) {
      return Container(
        width: constraints.maxWidth,
        decoration: widget.decoration,
        color: widget.backgroundColor,
        child: Align(
            child: Container(
                color: widget.valueColor,
                decoration: widget.valueDecoration,
                width: progressValue),
            alignment: Alignment.centerLeft),
      );
    } else if (type == 3) {
      return Row(
        children: [
          Text(
            widget.text ?? "",
            style: TextStyle(fontSize: valueFontSize, color: labelTextColor),
          ),
          const SizedBox(
            width: 8,
          ),
          Expanded(
            child: LayoutBuilder(
                builder: (BuildContext context, BoxConstraints constraints) {
              var progress = (widget.value ?? 0) * constraints.maxWidth;
              return Container(
                width: constraints.maxWidth,
                decoration: widget.decoration,
                color: widget.backgroundColor,
                child: Align(
                    child: Container(
                        color: widget.valueColor,
                        decoration: widget.valueDecoration,
                        width: progress),
                    alignment: Alignment.centerLeft),
              );
            }),
          ),
          Container(
              padding: const EdgeInsets.only(left: 5),
              width: calcTextSize("${widget.valueAlias ?? widget.maxValue}",
                  fontSize: valueFontSize),
              child: Text(
                widget.valueAlias ?? '$value',
                style:
                    TextStyle(fontSize: valueFontSize, color: valueTextColor),
              )),
        ],
      );
    } else {
      return Container(
          width: constraints.maxWidth,
          decoration: widget.decoration,
          color: widget.backgroundColor,
          child: Row(children: [
            Container(
                color: widget.valueColor,
                decoration: widget.valueDecoration,
                width: progressValue,
                child: widget.type == 0
                    ? Align(
                        child: Text(
                          '$value',
                          style: TextStyle(
                              fontSize: valueFontSize, color: valueTextColor),
                        ),
                        alignment: Alignment.centerRight)
                    : null),
            Align(
                child: widget.type == 1
                    ? Text(
                        '$value',
                        style: TextStyle(
                            fontSize: valueFontSize, color: valueTextColor),
                      )
                    : null,
                alignment: Alignment.centerLeft),
          ]),
          alignment: Alignment.centerLeft);
    }
  }

  _leftText() {
    if (widget.type == 4) {
      return Align(
          child: Container(
              alignment: Alignment.centerLeft,
              height: widget.height,
              child: Padding(
                child: Text(
                  widget.text ?? "",
                  style: TextStyle(
                      fontSize: (widget.height ?? 10) - 4,
                      color: labelTextColor),
                ),
                padding: EdgeInsets.only(left: 2),
              )),
          alignment: Alignment.centerLeft);
    } else if (widget.type > 0) {
      return Container();
    } else {
      return Align(
          child: Container(
              alignment: Alignment.centerLeft,
              height: widget.height,
              child: Padding(
                child: Text(
                  widget.text ?? "",
                  style: TextStyle(
                      fontSize: (widget.height ?? 10) - 4,
                      color: labelTextColor),
                ),
                padding: EdgeInsets.only(left: 2),
              )),
          alignment: Alignment.centerLeft);
    }
  }

  double calcTextSize(String text, {double? fontSize}) {
    final textSpan = TextSpan(
      text: text,
      style: TextStyle(fontSize: fontSize ?? 12, color: Colors.white),
    );
    final tp = TextPainter(text: textSpan, textDirection: TextDirection.ltr);
    tp.layout();
    return tp.width + 10;
  }
}
